<?php

namespace App\Console\Commands;

use App\Models\Admin\Supplier;
use App\Models\Mysql\XnOrderBill;
use App\Models\Mysql\XnOrderBillCensus;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class OrderCensus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:order-census{type}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '数据统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $type =  $this->argument('type');
        $strtime = time();
        $datestime = date('Y-m-d',strtotime("-1 day"));
        $datestime_arr = explode('-',$datestime);
        $time = [$datestime.' 00:00:00',$datestime.' 23:59:59'];
        $con[] = ['order_type','=',$type];
        $con[] = ['recharge_status','=',1];
        $condition = $con;
        $condition[] = ['bookkeeping_type','=',1];
        $where = $con;
        $where[] = ['bookkeeping_type','=',2];
        $supplier_info = Supplier::where('account_verification','=',1) -> get() -> toArray();
        foreach($supplier_info as $k => $v) {
            $connect = dbRegister($v['id']);
            //一天收入多少钱
            $NoInvoiceInfoIncome= DB::connection($connect) -> table('xn_order_bill')->where($condition)
                ->whereBetween('ctime',$time)
                ->selectRaw('COUNT(id) as allcount,SUM(budget_money) as allmoney')
                ->first();
            $income_money = $NoInvoiceInfoIncome['allmoney'];
            //一天收入多少笔
            $income_count = $NoInvoiceInfoIncome['allcount'];
            //一天支出多少钱
            $NoInvoiceInfoOut = DB::connection($connect) -> table('xn_order_bill')->where($where)
                ->whereBetween('ctime',$time)
                ->selectRaw('COUNT(id) as allcount,SUM(budget_money) as allmoney')
                ->first();
            $pay_money=$NoInvoiceInfoOut['allmoney'];
            //一天支出多少笔
            $pay_pen =$NoInvoiceInfoOut['allcount'];
            // 期初余额
            $a = DB::connection($connect) -> table('xn_order_bill')->where('order_type','=',$type)
                ->whereBetween('ctime',$time)
                ->oldest('ctime')
                ->limit(1)
                ->value('balance_before_change');
            // 期末余额
            $e =  DB::connection($connect) -> table('xn_order_bill')->where('order_type','=',$type)
                ->whereBetween('ctime',$time)
                ->latest('ctime')
                ->limit(1)
                ->value('balance_after_change');
            if($a){
                $b_money = $a;
            }else{
                $b_money = 0;
            }
            if($e){
                $e_money = $e;
            }else{
                $e_money = 0;
            }
            $data = [
                'year'=>$datestime_arr[0],
                'month'=>$datestime_arr[1],
                'day'=> $datestime_arr[2],
                'income_money' => $income_money ?? 0,
                'income_count'=> $income_count,
                'out_monoey' => $pay_money ?? 0,
                'out_count' =>$pay_pen,
                'begin_money' =>$b_money,
                'end_money' =>$e_money,
                'ctime'=>$strtime,
                'type' => $type
            ];
            try{
                $timewhere[] = ['year','=',$datestime_arr[0]];
                $timewhere[] = ['month','=',$datestime_arr[1]];
                $timewhere[] = ['day','=',$datestime_arr[2]];
                $timewhere[] = ['type','=',$type];
                $exit = DB::connection($connect) -> table('xn_order_bill_census')-> where($timewhere) -> exists();
                if(!$exit){
                    DB::connection($connect) -> table('xn_order_bill_census')->insert($data);
                }else{
                    DB::connection($connect) -> table('xn_order_bill_census')->where($timewhere) ->  update($data);
                }
                if($type == 1){
                    Log::channel('task')->info($datestime.$connect."库不开票数据统计执行成功\r\n");
                }elseif($type == 2){
                    Log::channel('task')->info($datestime.$connect."库开票数据统计执行成功\r\n");
                }

            }catch (\Exception $e){
                Log::channel('task')->info($datestime.$connect."库数据统计执行失败\r\n");
            }
        }

    }

}
